package com.lzb.watermarkcamera.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.util.TypedValue;
import android.view.MotionEvent;
import android.view.ViewConfiguration;
import android.widget.FrameLayout;

import androidx.annotation.Nullable;

import com.lzb.watermarkcamera.R;

public class DraggableViewHome extends FrameLayout {

    private int screenHeight;
    private int screenWidth;
    private int marginBottom;
    private float maxY;
    private float minY;  // 动态计算状态栏高度
    private float maxX;
    private float minX = 0;
    private float initialTouchX;
    private float initialTouchY;
    private float initialX;
    private float initialY;
    private int touchSlop;
    private boolean isDragging = false;
    private OnDragListener onDragListener;
    private OnClickListener onClickListener;

    public DraggableViewHome(Context context) {
        super(context);
        init(context, null);
    }

    public DraggableViewHome(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs);
    }

    public DraggableViewHome(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context, attrs);
    }

    private void init(Context context, AttributeSet attrs) {
        DisplayMetrics metrics = context.getResources().getDisplayMetrics();
        screenHeight = metrics.heightPixels;
        screenWidth = metrics.widthPixels;

        // 默认marginBottom为100dp
        marginBottom = (int) TypedValue.applyDimension(
                TypedValue.COMPLEX_UNIT_DIP, 100, metrics);

        // 读取自定义属性
        if (attrs != null) {
            TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.DraggableViewHome);
            marginBottom = a.getDimensionPixelSize(
                    R.styleable.DraggableViewHome_marginBottom, marginBottom);
            a.recycle();
        }

        // 计算状态栏高度
        minY = getStatusBarHeight(context)+50;
        touchSlop = ViewConfiguration.get(context).getScaledTouchSlop();
        setClickable(true);
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        // 计算最大坐标范围
        maxY = screenHeight - h - marginBottom;  // 底部限制
        maxX = screenWidth - w;                 // 右侧限制
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                initialTouchX = event.getRawX();
                initialTouchY = event.getRawY();
                initialX = getX();
                initialY = getY();
                isDragging = false;
                return true;
            case MotionEvent.ACTION_MOVE:
                float deltaX = event.getRawX() - initialTouchX;
                float deltaY = event.getRawY() - initialTouchY;

                if (!isDragging && 
                    (Math.abs(deltaX) >= touchSlop || Math.abs(deltaY) >= touchSlop)) {
                    isDragging = true;
                }

                if (isDragging) {
                    float newX = initialX + deltaX;
                    float newY = initialY + deltaY;
                    newX = Math.max(minX, Math.min(newX, maxX));
                    newY = Math.max(minY, Math.min(newY, maxY));  // 顶部限制为状态栏高度
                    setX(newX);
                    setY(newY);
//                    Config.TOP=getViewY();
//                    Config.LEFT=getViewX();
                    if (onDragListener != null) {
                        onDragListener.onDrag(newX, newY);
                    }
                    return true;
                }
                return false;
            case MotionEvent.ACTION_UP:
                if (!isDragging) {
                    if (onClickListener != null) {
                        onClickListener.onClick();
                    }
                    performClick();
                }
                return true;
            default:
                return super.onTouchEvent(event);
        }
    }

    // 获取状态栏高度
    private int getStatusBarHeight(Context context) {
        int result = 0;
        int resourceId = context.getResources().getIdentifier(
                "status_bar_height", "dimen", "android");
        if (resourceId > 0) {
            result = context.getResources().getDimensionPixelSize(resourceId);
        }
        return result;
    }

    // 自定义监听接口（与之前相同）
    public interface OnClickListener { void onClick(); }
    public interface OnDragListener { void onDrag(float x, float y); }

    public void setOnCustomClickListener(OnClickListener listener) {
        this.onClickListener = listener;
    }

    public void setOnDragListener(OnDragListener listener) {
        this.onDragListener = listener;
    }

    // 其他方法（获取位置等保持不变）
    public float getViewX() { return getX(); }
    public float getViewY() { return getY(); }
    public int[] getScreenPosition() {
        int[] location = new int[2];
        getLocationOnScreen(location);
        return location;
    }
} 